#include"miscFunction.h"

void drawLine(char ch,int length)
{
    cout << setw(length) << setfill(ch);
    cout << ch;
    cout << setfill(' ');
    cout << endl;
}

int validRating()
{
    string rating = " ";
    bool validOption = false;
    
    while(!validOption)
    {
        getline(cin,rating);
        
        if(rating.length() > 1)
            cout << "Error: Detected a Sentence! Re-enter Rating (Any Number from 1 to 5) : ";
        else
        {
            int vRating = atoi(rating.c_str());
            
            if(vRating > 5)
                cout << "Error: Rating is beyond 5!. Re-enter Rating (Any Number from 1 to 5) : ";
            else
            {
                validOption = true;
                return vRating;
            }
        }
    }
}

string encrypt(string in, int KEY)
{
	int temp;
	// resize KEY to fit ascii printable range
	KEY %= 95;	
  for(int i = 0; i < in.length(); i++)
	{
		// add KEY
		temp = (int)(in[i]) + KEY;
		
		// if not printable, wrap around
		if(temp > 126)
		{
			temp -= 126;
			temp += 31;
		}
		in[i] = (char)temp;
	}
  return in;
}

string decrypt(string in, int KEY)
{
	int temp;
	// resize KEY to fit ascii printable range
	KEY %= 95;	
  for(int i = 0; i < in.length(); i++)
	{
		// subtract KEY
		temp = (int)(in[i]) - KEY;
		
		// if not printable, wrap around
		if(temp < 32)
		{
			if(temp > 0)
				temp = 32 - temp;
			else
				temp = (temp * -1) + 32;
			temp = 127 - temp;
		}
		in[i] = (char)temp;
	}
  return in;
}

string charStrToIntStr(string in)
{
	int temp;
	stringstream out;
	
	for(int i = 0; i < in.length(); i++)
	{
		if((int)in[i] < 100)
			out << 0 << (int)in[i];
		else
			out << (int)in[i];
		if(i < in.length()-1)
			out << ',';
	}
  return out.str();
}

string intStrToCharStr(string in)
{
	int temp;
	string out;
	
	string::size_type pos = 0;
	do{
		if(pos == 0)
			temp = atoi(in.substr(pos, 3).c_str());
		else
			temp = atoi(in.substr(pos+1, 3).c_str());
		out += (char)temp;
		pos = in.find_first_of(',', pos+2);
	}while(pos != string::npos);
	
  return out;
}

int getCurrentYear()
{
	time_t theTime = time(NULL);
	struct tm *aTime = localtime(&theTime);

	return (aTime->tm_year - 100); // Year is # years since 1900
}

string getCurrentMonth()
{
	string monthArray[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	
	time_t theTime = time(NULL);
	struct tm *aTime = localtime(&theTime);

	return (monthArray[aTime->tm_mon]); // Month is 0 - 11
}

int getCurrentDay()
{
	time_t theTime = time(NULL);
	struct tm *aTime = localtime(&theTime);

	return (aTime->tm_mday);
}